package com.aispeech.astools.view;

import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.aispeech.astools.R;
import com.aispeech.astools.utils.SystemInfoUtils;
import java.util.ArrayList;
import java.util.HashMap;

import androidx.appcompat.app.AppCompatActivity;

public class SystemInfoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_info);

        //android version, api,kernel version,
        //chip, cores, clock speed,
        //screen size, screen density
        //total ram / ava ram, total rom/ava rom

        ListView listView = findViewById(R.id.lv_sys_infos);
        ArrayList<HashMap<String, String>> listItems = new ArrayList<>();

        HashMap<String, String> item;

        item = new HashMap<>();
        item.put("info_name", "Android Version");
        item.put("info_value", Build.VERSION.RELEASE);
        listItems.add(item);

        item = new HashMap<>();
        item.put("info_name", "API Level");
        item.put("info_value", Build.VERSION.SDK_INT + "");
        listItems.add(item);

        item = new HashMap<>();
        item.put("info_name", "Kernel");
        item.put("info_value", System.getProperty("os.version"));
        listItems.add(item);

        item = new HashMap<>();
        item.put("info_name", "Brand");
        item.put("info_value", Build.BRAND);
        listItems.add(item);

        item = new HashMap<>();
        item.put("info_name", "Model");
        item.put("info_value", Build.MODEL);
        listItems.add(item);

        float totalRam = SystemInfoUtils.getTotalRam(this);
        item = new HashMap<>();
        item.put("info_name", "Total Ram");
        item.put("info_value", String.format("%.2f", totalRam) + " MB");
        listItems.add(item);

        float availableRam = SystemInfoUtils.getAvailableRam(this);
        item = new HashMap<>();
        item.put("info_name", "Available Ram");
        item.put("info_value", String.format("%.2f", availableRam) + " MB" + "(" + String.format("%.2f", availableRam / totalRam * 100) + " %)");
        listItems.add(item);

        float totalStorage = SystemInfoUtils.getTotalStorage();
        item = new HashMap<>();
        item.put("info_name", "Total Storage");
        item.put("info_value", String.format("%.2f", totalStorage) + " GB");
        listItems.add(item);

        float availableStorage = SystemInfoUtils.getAvailableStorage();
        item = new HashMap<>();
        item.put("info_name", "Available Storage");
        item.put("info_value", String.format("%.2f", availableStorage) + " GB" + "(" + String.format("%.2f", availableStorage / totalStorage * 100) + " %)");
        listItems.add(item);

        int cores = SystemInfoUtils.getCores();
        item = new HashMap<>();
        item.put("info_name", "Cores");
        item.put("info_value", cores + "");
        listItems.add(item);

        for (int i = 0; i < cores; i++) {
            float clockMinSpeed = SystemInfoUtils.getClockMinSpeed("cpu" + i);
            float clockMaxSpeed = SystemInfoUtils.getClockMaxSpeed("cpu" + i);
            item = new HashMap<>();
            item.put("info_name", "cpu " + i);
            item.put("info_value", clockMinSpeed / 1000 + " MHz - " + clockMaxSpeed / 1000 / 1000 + " GHz");
            listItems.add(item);
        }

        item = new HashMap<>();
        item.put("info_name", "Screen Size");
        item.put("info_value", String.format("%.2f", SystemInfoUtils.getScreenSize(this)) + " inches");
        listItems.add(item);

        // set screen resolution
        DisplayMetrics metrics1 = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(metrics1);
        item = new HashMap<>();
        item.put("info_name", "Screen Resolution");
        item.put("info_value", metrics1.heightPixels + " x " + metrics1.widthPixels + " pixels");
        listItems.add(item);

        // set density
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int densityDpi = (int) (metrics.density * 160f);
        item = new HashMap<>();
        item.put("info_name", "Screen Density");
        item.put("info_value", densityDpi + " dpi");
        listItems.add(item);

        SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_info_item,
            new String[]{"info_name", "info_value"}, new int[]{R.id.tv_name, R.id.tv_value});

        listView.setAdapter(adapter);
        listView.setDividerHeight(2);
    }


}
